package com.hanfeng.client.entity;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

/**
 * 查询音乐集合
 * 
 * @author hanfeng
 * @data 2012-8-30 下午3:00:14
 */
public class MusicList {
	public static List<Music> getMusicDate(Context context) {
		List<Music> musicList = new ArrayList<Music>();
		ContentResolver contentResolver = context.getContentResolver();
		if (contentResolver != null) {
			// 返回所有在外部存储卡上的音乐文件的信息
			Cursor cursor = contentResolver.query(
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
					null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if (cursor == null) {
				return null;
			}
			if (cursor.moveToFirst()) {
				do {
					Music music = new Music();
					// 歌曲ID
					int id = cursor.getInt(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
					// 歌曲名称
					String title = cursor
							.getString(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
					// 歌曲专辑
					String album = cursor
							.getString(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
					// 歌手
					String singer = cursor
							.getString(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
					// 歌曲路径
					String url = cursor
							.getString(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
					// 播放时长
					long time = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
					// 歌曲文件大小
					long size = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
					// 歌曲文件名
					String musicName = cursor
							.getString(cursor
									.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
					String sbr = musicName.substring(musicName.length() - 3,
							musicName.length());
					if (sbr.equals("mp3")) {
						music.setId(id);
						music.setTitle(title);
						music.setSinger(singer);
						music.setAlbumName(album);
						music.setSize(size);
						music.setTime(time);
						music.setUrl(url);
						music.setMusicName(musicName);
						musicList.add(music);	
					}
				} while (cursor.moveToNext());
			}
		}
		return musicList;
	}
}
